
"use strict";

angular.module('app.protocol').controller('AllProtocolCtrl', function ($scope,$http,$cookieStore) {
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [1, 2, 3, 40, 50],
        onChange: function () {
        }
    };
	$scope.get_list = function(){
		var page_obj = {
			page:$scope.paginationConf.currentPage,
			rows:$scope.paginationConf.itemsPerPage
		}
		$http.post(baseurl+'/list/select/protocol',page_obj).then(function(res){
			$scope.datas = res.data.rows;
			$scope.paginationConf.totalItems = res.data.records;
		})
	}
	$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.get_list);
	// 添加
	$scope.addobj = {};
	$scope.contentlen = {};
	$scope.cmd = {};
	$scope.deviceid = {};
	$scope.headfields = {};
	$scope.other = {};
	$scope.crc = {};
	$scope.contentlen.includeht = '0';
    $scope.sure_add = function(){
    	$scope.addobj.contentlen = JSON.stringify($scope.contentlen);
    	$scope.addobj.cmd = JSON.stringify($scope.cmd);
    	$scope.addobj.deviceid = JSON.stringify($scope.deviceid);
        $scope.addobj.other = JSON.stringify($scope.other);
        if($scope.addobj.name && $scope.contentlen.offset && $scope.contentlen.len && $scope.contentlen.includeht && $scope.cmd.offset && $scope.cmd.len && $scope.deviceid.offset && $scope.deviceid.len){
            $http.post(baseurl+'/create_protocol',$scope.addobj).then(function(res){
                if (res.data.ret == 1) {
                    $("#addAlert").modal("hide");
                    $scope.get_list();
                }else{
                    console.log("插入出错");
                }
            });
		}else {
            new Notify("fail","有空值未填写");
		}

    };
	// // 修改
    $scope.showEditAlert = function (item) {
        $scope.cmd_edit = JSON.parse(item.cmd);
        $scope.contentlen_edit = JSON.parse(item.contentlen);
        $scope.deviceid_edit = JSON.parse(item.deviceid);
        $scope.other_edit = JSON.parse(item.other);
        $scope.editobj = item;
        $scope.editid = item.id;
	}
    $scope.sure_edit = function(){
        delete $scope.editobj.id;
        $scope.editobj.cmd = JSON.stringify($scope.cmd_edit);
        $scope.editobj.contentlen = JSON.stringify($scope.contentlen_edit);
        $scope.editobj.deviceid = JSON.stringify($scope.deviceid_edit);
        $scope.editobj.other = JSON.stringify($scope.other_edit);
        if ($scope.editobj.name && $scope.editobj.port && $scope.other_edit.offset && $scope.other_edit.len && $scope.contentlen_edit.offset && $scope.contentlen_edit.len && $scope.contentlen_edit.includeht && $scope.cmd_edit.offset && $scope.cmd_edit.len && $scope.deviceid_edit.offset && $scope.deviceid_edit.len) {
            $http.put(baseurl + '/data/protocol?id==' + $scope.editid, $scope.editobj).then(function (res) {
                if (res.data.ret == 1) {
                    $("#editAlert").modal("hide");
                    $scope.get_list();
                } else {
                    console.log("编辑出错");
                }
            });
        } else {
            new Notify("fail", "有空值未填写");
        }
    };

	// 删除
	$scope.showDelDialog = function(id,port){
		$scope.delid = id;
        $scope.delport = port;
	};
	$scope.sure_del = function(){
    	$http.delete(baseurl+'/sensor/protocol?id=='+$scope.delid+"&port="+$scope.delport).then(function(res){
            if (res.data.ret == 1) {
            	 $("#delAlert").modal("hide");
            	 $scope.get_list();
            }
    	});
	};
    // 编辑协议数据表
    $scope.editProtocol = function(port){
        window.location.href = '#/editProtocol?port='+port;
    }

});